//
//  LoginViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/3/25.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "LoginViewC.h"
#import "RegistViewC.h"
#import "MemberCenterViewC.h"
#import "UserModel.h"
#import "Util.h"
#import <MBProgressHUD.h>
#import "FindPassViewC.h"

@interface LoginViewC ()

@end

@implementation LoginViewC

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =@"登录";
    _passField.secureTextEntry = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(autoLogin:)
                                           name:@"NOTIFICATION_AUTOLOGIN"
                                               object:nil];
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]
                                 initWithImage:[UIImage imageNamed:@"01"]
                                 style:UIBarButtonItemStylePlain
                                target:self action:@selector(dismiss:)];
    self.navigationItem.leftBarButtonItem =leftBtn;
}

- (void)dismiss:(id)sender{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)autoLogin:(NSNotification *)noti {
    NSDictionary *dic = [noti object];
    _nameField.text = dic[@"loginName"];
    _passField.text = dic[@"password"];
    
    [self loginBtn:nil];
}

- (IBAction)registBtn:(id)sender{
    RegistViewC *registvc = [[RegistViewC alloc]
                             initWithNibName:@"RegistViewC"
                             bundle:nil];
    [self.navigationController pushViewController:registvc animated:YES];

}

- (IBAction)loginBtn:(id)sender{
    
    [self.view endEditing:YES];
    NSString *name = _nameField.text;
    NSString *password = _passField.text;
    if (name.length == 0 || password.length == 0) {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.labelText = @"请将用户名和密码填写完整";
        [HUD hide:YES afterDelay:1.f];
        return ;
    }
    [Util changeNSString:name];
    [Util changeNSString:password];
    
        __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[UserModel sharedInstance] loginWithLoginname:name password:password
    successed:^(BOOL successed, unsigned int value) {
        if (successed) {
            hud.detailsLabelText = @"登陆成功";
            [hud hide:YES afterDelay:1.5f];
            [self dismissViewControllerAnimated:YES completion:^{
            }];
        }
        else{
            hud.detailsLabelText = @"用户名或密码错误";
            [hud hide:YES afterDelay:1.5f];
        }
        
    }];
    
}

- (IBAction)forgerPassBtn:(id)sender{
    FindPassViewC *findvc = [[FindPassViewC alloc] initWithNibName:@"FindPassViewC"
                                                            bundle:nil];
    [Util presentNaviC:findvc fromController:self];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    return YES;
}

@end
